package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.ActivityLogPageDTO;
import com.nnnu.wsnackshop.pojo.vo.ActivityLogVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IActivityLogsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台日志 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/api/admin/activity-logs")
@Slf4j
@Validated
@Tag(name = "管理端操作日志接口")
@RequiredArgsConstructor
public class ActivityLogsController {

    private final IActivityLogsService logService;

    @Operation(summary = "分页查询操作日志", description = "仅 superAdmin 或 admin 且拥有 log:read 权限可访问")
    @GetMapping
    @RequiresRoles(value = {"superAdmin","admin"}, logical = Logical.OR)
    @RequiresPermissions("log:read")
    public Result<?> pageLogs(@Valid @ParameterObject ActivityLogPageDTO dto) {
        log.info("管理员分页查询日志：{}", dto);
        return Result.success(logService.pageLogs(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取操作日志详情", description = "仅 superAdmin 或 admin 且拥有 log:read 权限可访问")
    @GetMapping("/{id}")
    @RequiresRoles(value = {"superAdmin","admin"}, logical = Logical.OR)
    @RequiresPermissions("log:read")
    public Result<ActivityLogVO> getLog(
            @PathVariable @Parameter(description = "日志ID", in = ParameterIn.PATH) Long id) {
        log.info("管理员获取日志详情：id={}", id);
        return Result.success(logService.getLogDetail(id),MessageConstants.SELECT_OK);
    }

    @Operation(summary = "批量删除操作日志", description = "仅 superAdmin 且拥有 log:delete 权限可访问")
    @DeleteMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("log:delete")
    public Result<?> deleteLogs(@RequestParam @Parameter(description = "删除日志ID列表",  in = ParameterIn.QUERY) List<Long> ids) {
        log.info("管理员批量删除日志：ids={}", ids);
        logService.deleteLogs(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}
